package com.dofast.bt.mvp.view.activity.basket;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.content.Loader;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.AdapterView;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.dofast.bt.R;
import com.dofast.bt.base.BaseMvpActivity;
import com.dofast.bt.bean.GoodsBean;
import com.dofast.bt.bean.ShopBean;
import com.dofast.bt.mvp.presenter.basket.StoreDetialPresenter;
import com.dofast.bt.mvp.view.activity.shop.TeaDetailActivity;
import com.dofast.bt.util.GlideImgManager;
import com.dofast.bt.util.Helper;
import com.dofast.bt.util.PresenterFactory;
import com.dofast.bt.util.PresenterLoder;
import com.dofast.bt.util.Utility;
import com.dofast.bt.widget.MyGridview;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2016/12/20.
 */

public class StoreDetailActivity extends BaseMvpActivity<StoreDetialPresenter, StoreDetailView> implements StoreDetailView, AdapterView.OnItemClickListener {
    @BindView(R.id.ll_flag)
    LinearLayout llFlags;
    @BindView(R.id.iv_bg_detail)
    ImageView ivBgDetail;
    @BindView(R.id.iv_bg_detail_center)
    ImageView ivBgDetailCenter;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_store_name)
    TextView tvStoreName;
    @BindView(R.id.tv_gz_num)
    TextView tvGzNum;
    @BindView(R.id.btn_gz)
    TextView btnGz;
    @BindView(R.id.tv_detail)
    TextView tvDetail;
    @BindView(R.id.ll_online_goods)
    LinearLayout llOnlineGoods;
    @BindView(R.id.gv_goods)
    MyGridview gvGoods;

    @Override
    protected int getResourcesId() {
        return R.layout.activity_store_detail;
    }

    @Override
    protected void initView() {
        initTitle();
        presenter.initData();
    }

    @Override
    protected void addOnClickListener() {
        gvGoods.setOnItemClickListener(this);
    }

    @Override
    public Loader<StoreDetialPresenter> onCreateLoader(int i, Bundle bundle) {
        return new PresenterLoder<>(this, new PresenterFactory<StoreDetialPresenter>() {
            @Override
            public StoreDetialPresenter create() {
                return new StoreDetialPresenter();
            }
        });
    }

    @Override
    public void initTitle() {

    }

    @Override
    public void gotoActivity(Class<?> tClass, boolean isFinish) {

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @OnClick({R.id.iv_back, R.id.btn_gz, R.id.tv_detail})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.btn_gz:
                // TODO: 2016/12/20  关注逻辑处理
//                presenter.addConcern();
                break;
            case R.id.tv_detail:
                // TODO: 2016/12/20 更多逻辑处理
                break;
        }
    }

    @Override
    public void initViewData(ShopBean shopBean, boolean isAttention) {
        if (isAttention) {
            btnGz.setText("已关注");
//            btnGz.setEnabled(false);
        } else {
            btnGz.setText("+ 关注");
//            btnGz.setEnabled(true);
        }
        tvStoreName.setText(shopBean.getName());
        tvGzNum.setText(shopBean.getConcerns() + "");
        if (shopBean.getImg_head() != null) {
            GlideImgManager.glideLoader(this, shopBean.getImg_head(), R.mipmap.bg_store_dtail_icon, R.mipmap.bg_store_dtail_icon, ivBgDetailCenter, 0);
//            Glide.with(this).load(shopBean.getImg_head()).into(ivBgDetailCenter);
        }
        if (shopBean.getImg_bg() != null) {
            Glide.with(this).load(shopBean.getImg_bg()).into(ivBgDetail);
        }
        tvDetail.setText(shopBean.getContent());
        llFlags.removeAllViews();
        if (shopBean.getFlag() != null) {
            String[] flags = shopBean.getFlag().split("、");
            for (int i = 0; i < flags.length; i++) {
                llFlags.addView(getFlag(flags[i]));
            }

        }
    }

    @Override
    public void initAdapter(Adapter adapter) {
        gvGoods.setAdapter((ListAdapter) adapter);
    }

    @Override
    public void gotoTeaDetailActivity(GoodsBean goodsBean) {
        TeaDetailActivity.startTeaDetailActivity(StoreDetailActivity.this, goodsBean.getGoodsId());
    }

    static final String SHOP_ID = "shopId";

    public static void goTo(Context context, int shopId) {
        Intent intent = new Intent(context, StoreDetailActivity.class);
        intent.putExtra(SHOP_ID, shopId);
        context.startActivity(intent);
    }

    @Override
    public int getShopId() {
        return getIntent().getIntExtra(SHOP_ID, 0);
    }

    @Override
    public void setStatus(String status) {
        btnGz.setText(status);
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        presenter.itemClick(position);
    }

    private View getFlag(String name) {
        View view = LayoutInflater.from(this).inflate(R.layout.item_textview_flags, null);
        TextView textView = (TextView) view.findViewById(R.id.name);
        textView.setText(name);
        return view;
    }

}
